package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.CustomerDrugProjectExe;
import com.lmx.repoDomain.tmodel.Nursingandprojectexe;
import com.lmx.repoDomain.tmodel.NursingPlanExecute;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(Nursingandprojectexe.class)
@Component
public class NursingandprojectexeRepository extends BaseRepository<Nursingandprojectexe> {

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(Nursingandprojectexe.class);
	}

	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private NursingService nursingService;
	
	@Override
	@JsonApiSave
	public Nursingandprojectexe save(Nursingandprojectexe exe) {
		Map<String, List<Object>> map = ActionContextHolder.getContext().getPayloadDatas();
		List<Object> projectsObj = map.get("projectexes");
		List<Object> nursingsObj = map.get("nursingexes");

		List<CustomerDrugProjectExe> projects = new ArrayList<CustomerDrugProjectExe>();
		for (Object object : projectsObj) {
			projects.add((CustomerDrugProjectExe) object);
		}
		
		List<NursingPlanExecute> nursings = new ArrayList<NursingPlanExecute>();
		for (Object object : nursingsObj) {
			nursings.add((NursingPlanExecute) object);
		}
		exe.setNursingexes(nursings);
		exe.setProjectexes(projects);
		for (NursingPlanExecute data : exe.getNursingexes()) {
			if (data.getId() == null) {
				if (data.getDetail() == null && data.getItemProject() == null) {
					throw new BusinessException(null, CommonConstants.ERROR_CODE.DATA_ERROR.label(), null);
				}
				//定时任务 如果已经被执行加验证
				if(data.getDetail()!=null){
					int addFlag = nursingService.checkPlanDetailExeRelations(data.getDetail());
					if (addFlag > 0) {
						data.setErrcode(14);
						throw new BusinessException(null, CommonConstants.ERROR_CODE.REPEAT_ERROR.label(), null);
					}
				}
				nursingService.addPlanExe(data);
			} else {
				if (data.getDelStatus() == 1) {
					//判断data是否挂接warning 有则无法删除
					if (data.getWarning() != null) {
						data.setErrcode(8);
						throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
					}
					//同时删除相应的增值服务账单
					nursingService.deleteNursingPlanExecute(data);
				} else {
					nursingService.addPlanExe(data);
				}
			}
		}
		for (CustomerDrugProjectExe data : exe.getProjectexes()) {
			data = nursingService.operateCustomerDrugProjectExe(data);
			// 药量不足
			if (data.getErrcode() != null && data.getErrcode().intValue() == 14) {
				throw new BusinessException(null, CommonConstants.ERROR_CODE.ADD_ERROR.label(), null);
			}
			// 重复执行
			if (data.getErrcode() != null && data.getErrcode().intValue() == 15) {
				throw new BusinessException(null, CommonConstants.ERROR_CODE.REPEAT_ERROR.label(), null);
			}
		}
		return exe;
	}
}
